package cn.iocoder.yudao.module.mon.dal.redis.ys7auth;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.mon.dal.dataobject.Ys7AccessTokenResDO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Repository
public class Ys7AccessTokenRedisDAO {


        @Resource
        private StringRedisTemplate stringRedisTemplate;

        public Ys7AccessTokenResDO get(String accessToken) {
            String redisKey = formatKey(accessToken);
            return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), Ys7AccessTokenResDO.class);
        }

        public void set(Ys7AccessTokenResDO accessTokenDO) {
            String redisKey = formatKey(accessTokenDO.getAccessToken());
            stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(accessTokenDO),
                    accessTokenDO.getExpireTime()- System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        }

        public void delete(String accessToken) {
            String redisKey = formatKey(accessToken);
            stringRedisTemplate.delete(redisKey);
        }

        public void deleteList(Collection<String> accessTokens) {
            List<String> redisKeys = CollectionUtils.convertList(accessTokens, Ys7AccessTokenRedisDAO::formatKey);
            stringRedisTemplate.delete(redisKeys);
        }

        private static String formatKey(String accessToken) {

//            return YS7_ACCESS_TOKEN.getKeyTemplate();
            return "";
        }

    }
